
package cn.pconline.autoclub.util.app;

import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 *
 * @author pc
 */
public class AppControllerStatic {
	public static final String APP_API_PARA_CONTENT = "content";//内容
	public static final String APP_API_PARA_ALBUMID = "albumId";//相册ID
	public static final String APP_API_PARA_PHOTOID = "photoId";//照片ID
	public static final String APP_API_PARA_ACTIVITYID = "activityId";//活动ID
    public static final String APP_API_PARA_USERID = "userId";//用户ID
    public static final String APP_API_PARA_CLUBID = "clubId";//车友会ID
    public static final String APP_API_PARA_CLUBURL = "clubUrl";//车友会Url
    public static final String APP_API_PARA_PROVINCEID = "provinceId";//省份ID
    public static final String APP_API_PARA_CITYID = "cityId";//城市ID
    public static final String APP_API_PARA_BRANDID = "brandId";//品牌ID
    public static final String APP_API_PARA_SERIESID = "seriesId";//车系ID
    public static final String APP_API_PARA_XSSENCODE = "xssEncode";//0 原内容 1 过滤xss
    public static final String APP_API_PARA_DYNAINFOID = "dynaInfoId";//动态ID
    public static final String APP_API_PARA_CALLBACK = "callback";//回调方法
    public static final String APP_API_PARA_PAGENO = "pageNo";//第几分页
    public static final String APP_API_PARA_PAGESIZE = "pageSize";//每页数量
    public static final String APP_API_PARA_PAGECOUNT = "pageCount";//总页数
    public static final String APP_API_PARA_TOTAL = "total";//返回数据总数
    public static final int APP_API_AUTHORPHOTO_SIZE_50 = 50;//作者头像50X50
    public static final int APP_API_DEF_PAGE_SIZE = 20;//默认每页数量
    public static final int APP_API_MIN_PAGE_SIZE = 1;//最小每页数量
    public static final int APP_API_MAX_PAGE_SIZE = 100;//最大每页数量
    public static final int APP_API_DEF_PAGE_NO = 1;//默认分页数
    public static final int APP_API_MAX_PAGE_NO = 100;//最大可查的分页数
    public static final int APP_API_DEF_XSSENCODE = 0;//默认返回原内容，不过滤xss
    public static final int STATUS_SUCCESS = 0;//成功调用接口
    public static final int STATUS_FAULT = -1;//调用接口失败
    public static final int STATUS_FAULT_LOGIN = -2;//登录验证失败
    public static final String MESSAGE_ERROR_LOGIN = "用户未登录，请先登录";
    public static final String WINDOW_NAME = "windowname";  //windowname 跨域参数
    public static final int IS_WINDOW_NAME_CROSS = 1; // WINDOWNAME跨域
    //版本提示
    public static final String V1_VERSION = "v1.0";//当前接口版本号1.0
    public static final String V1_VERSION_LATEST = "v1.0(latest)";// 当前接口版本号1.0为最新的版本
    public static final String V2_VERSION = "v2.0";//当前接口版本号2.0
    public static final String V2_VERSION_LATEST = "v2.0(latest)";// 当前接口版本号2.0为最新的版本
    public static final String V3_VERSION = "v3.0";//当前接口版本号2.0
    public static final String V3_VERSION_LATEST = "v3.0(latest)";// 当前接口版本号3.0为最新的版本
    
    public static final String UPC_COMMAND_PHOTO = "command=53003&command=53004&command=53016&command=53017&command=53018&command=53019&command=53020&command=53030&command=53031&command=53036&command=53037";
    public static final String UPC_COMMAND_ACTIVITY_COVER = "command=53006&command=53031&command=53034&command=53004";
    public static final String UPC_COMMAND_ACTIVITY_CONTENT = "command=53035";
	    
    //APP帖子链接协议
    public static final String APP_BBS_TOPIC_URL = "pcautoclub://bbs-topic/";
    
    
    /**
     * 接口返回相关的Json数据
     * @param response
     * @param str
     * @param callback
     */
	public static void printMsg(HttpServletResponse response, String str, String callback) {
		try {
			PrintWriter writer = response.getWriter();
			if (StringUtils.isNotBlank(callback)) {
				writer.print(EncodeUtils.encodeForHTMLFilterNull(callback) + "(" + str + ")");
			} else {
				writer.print(str);
			}
			writer.flush();
			writer.close();
		} catch (IOException ex) {
		}
	}
	
	
	/**
	 * 成功获取接口数据，不分页，默认Message
	 * @param response
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printSuccessMsg(HttpServletResponse response, 
			JSONObject jsonObject, String callback, String version) {
		printSuccessMsg(response, "", jsonObject, callback, version);
	}

	/**
	 * 成功获取接口数据，不分页
	 * @param resp
	 * @param message
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printSuccessMsg(HttpServletResponse response, String message, 
			JSONObject jsonObject, String callback, String version) {
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_SUCCESS);
		if (StringUtils.isNotEmpty(message)) {
			jsonObject.put("message", message);
		}else {
			jsonObject.put("message", "操作成功");
		}
		jsonObject.put("version", version);
		printMsg(response, jsonObject.toJSONString(), callback);
	}
	
	/**
	 * 返回接口数据（用于windowname跨域）
	 * @param response
	 * @param message
	 * @param jsonObject
	 * @param callback
	 * @param version
	 * @param windowName
	 */
	public static void printSuccessWithWindowName(HttpServletResponse response, String message, 
			JSONObject jsonObject, String callback, String version,int windowName) {
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_SUCCESS);
		if (StringUtils.isNotEmpty(message)) {
			jsonObject.put("message", message);
		}else {
			jsonObject.put("message", "操作成功");
		}
		jsonObject.put("version", version);
		
		if(windowName==IS_WINDOW_NAME_CROSS){
			printMsg(response,"<script>window.name= '" + jsonObject.toJSONString()+"';</script>", callback);
		}else{
			printMsg(response, jsonObject.toJSONString(), callback);
		}
	}
	
	/**
	 * 成功获取接口数据，分页，默认Message
	 * @param response
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printSuccessMsgPager(HttpServletResponse response, 
			JSONObject jsonObject, String callback, Pager<?> pager, String version) {
		printSuccessMsgPager(response, "", jsonObject, callback, pager, version);
	}
	
	/**
	 * 成功获取接口数据，分页
	 * @param resp
	 * @param message
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printSuccessMsgPager(HttpServletResponse response, String message, 
			JSONObject jsonObject, String callback, Pager<?> pager, String version) {
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_SUCCESS);
		if (StringUtils.isNotEmpty(message)) {
			jsonObject.put("message", message);
		}else {
			jsonObject.put("message", "操作成功");
		}
		jsonObject.put("version", version);
		//支持分页
		if(pager != null && pager.getResultList() != null && !pager.getResultList().isEmpty()) {
			jsonObject.put(AppControllerStatic.APP_API_PARA_TOTAL, pager.getTotal());
			jsonObject.put(AppControllerStatic.APP_API_PARA_PAGENO, pager.getPageNo());
			jsonObject.put(AppControllerStatic.APP_API_PARA_PAGESIZE, pager.getPageSize());
			jsonObject.put(AppControllerStatic.APP_API_PARA_PAGECOUNT, pager.getPageCount());
		} else {//分页接口没有数据时，返回默认的分页相关属性
			jsonObject.put(AppControllerStatic.APP_API_PARA_TOTAL, 0);
			jsonObject.put(AppControllerStatic.APP_API_PARA_PAGENO, 1);
			jsonObject.put(AppControllerStatic.APP_API_PARA_PAGESIZE, APP_API_DEF_PAGE_SIZE);
			jsonObject.put(AppControllerStatic.APP_API_PARA_PAGECOUNT, 0);
		}
		printMsg(response, jsonObject.toJSONString(), callback);
	}
	
	/**
	 * 获取接口数据失败，不分页，默认Message
	 * @param response
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printErrorMsg(HttpServletResponse response,
			JSONObject jsonObject, String callback, String version) {
		printErrorMsg(response, "", jsonObject, callback, version);
	}

	/**
	 * 获取接口数据失败，不分页
	 * @param resp
	 * @param code
	 * @param message
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printErrorMsg(HttpServletResponse response, String message,
			JSONObject jsonObject, String callback, String version) {
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_FAULT);
		if (StringUtils.isNotEmpty(message)) {
			jsonObject.put("message", message);
		} else {
			jsonObject.put("message", "操作失败");
		}
		jsonObject.put("version", version);
		printMsg(response, jsonObject.toJSONString(), callback);
	}
	
	/**
	 * 获取接口数据失败，不分页
	 * @param resp
	 * @param code
	 * @param message
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printLoginErrorMsg(HttpServletResponse response,
			JSONObject jsonObject, String callback, String version) {
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_FAULT);
		jsonObject.put("message", MESSAGE_ERROR_LOGIN);
		jsonObject.put("version", version);
		printMsg(response, jsonObject.toJSONString(), callback);
	}
	
	public static void printErrorWithWindowName(HttpServletResponse response, String message,
			JSONObject jsonObject, String callback, String version,int windowName){
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_FAULT);
		if (StringUtils.isNotEmpty(message)) {
			jsonObject.put("message", message);
		} else {
			jsonObject.put("message", "操作失败");
		}
		jsonObject.put("version", version);
		if(windowName==IS_WINDOW_NAME_CROSS){
			printMsg(response,"<script>window.name= '" + jsonObject.toJSONString()+"';</script>", callback);
		}else{
			printMsg(response, jsonObject.toJSONString(), callback);
		}
	}
	
	
	/**
	 * 获取接口数据失败，分页，默认Message
	 * @param response
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printErrorMsgPager(HttpServletResponse response,
			JSONObject jsonObject, String callback, String version) {
		printErrorMsgPager(response, "", jsonObject, callback, version);
	}

	/**
	 * 获取接口数据失败，分页
	 * @param resp
	 * @param code
	 * @param message
	 * @param jsonObject
	 * @param jsonArray
	 * @param callback
	 */
	public static void printErrorMsgPager(HttpServletResponse response, String message,
			JSONObject jsonObject, String callback, String version) {
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("code", STATUS_FAULT);
		if (StringUtils.isNotEmpty(message)) {
			jsonObject.put("message", message);
		} else {
			jsonObject.put("message", "操作失败");
		}
		jsonObject.put("version", version);
		// 分页相关默认属性
		jsonObject.put(AppControllerStatic.APP_API_PARA_TOTAL, 0);
		jsonObject.put(AppControllerStatic.APP_API_PARA_PAGENO, 1);
		jsonObject.put(AppControllerStatic.APP_API_PARA_PAGESIZE, APP_API_DEF_PAGE_SIZE);
		jsonObject.put(AppControllerStatic.APP_API_PARA_PAGECOUNT, 0);

		printMsg(response, jsonObject.toJSONString(), callback);
	}
	

	/**
	 * 异常处理，统一跳转到404页面
	 * @param response
	 * @return
	 */
	public static String redirect404(HttpServletResponse response) {
		try {
			response.sendError(404);
		} catch (Exception ex) {
		}
		return "/template/common/notFoundPage";
	}
	
	/**
	 * 参数pageSize
	 * @param request
	 * @return
	 */
	public static int paramPageSize(HttpServletRequest request) {
		int pageSize = WebUtils.paramInt(request,
				AppControllerStatic.APP_API_PARA_PAGESIZE,
				AppControllerStatic.APP_API_DEF_PAGE_SIZE,
				AppControllerStatic.APP_API_MIN_PAGE_SIZE,
				AppControllerStatic.APP_API_MAX_PAGE_SIZE);
		return pageSize;
	}

	/**
	 * 参数pageNo
	 * @param request
	 * @return
	 */
	public static int paramPageNo(HttpServletRequest request) {
		int pageNo = WebUtils.paramInt(request,
				AppControllerStatic.APP_API_PARA_PAGENO,
				AppControllerStatic.APP_API_DEF_PAGE_NO);
		return pageNo;
	}
	
	/**
	 * 参数callback
	 * @param request
	 * @return
	 */
	public static String paramCallback(HttpServletRequest request) {
		return WebUtils.param(request, AppControllerStatic.APP_API_PARA_CALLBACK, "");
	}
	
	/**
	 * 参数userId
	 * @param request
	 * @return
	 */
	public static long paramUserId(HttpServletRequest request) {
		return WebUtils.paramLong(request, AppControllerStatic.APP_API_PARA_USERID, 0);
	}
	
	/**
	 * 参数clubId
	 * @param request
	 * @return
	 */
	public static long paramClubId(HttpServletRequest request) {
		return WebUtils.paramLong(request, AppControllerStatic.APP_API_PARA_CLUBID, 0);
	}
	
	/**
	 * 参数provinceId
	 * @param request
	 * @return
	 */
	public static long paramProvinceId(HttpServletRequest request) {
		return WebUtils.paramInt(request, AppControllerStatic.APP_API_PARA_PROVINCEID, 0);
	}
	
	/**
	 * 参数cityId
	 * @param request
	 * @return
	 */
	public static long paramCityId(HttpServletRequest request) {
		return WebUtils.paramInt(request, AppControllerStatic.APP_API_PARA_CITYID, 0);
	}
	
	/**
	 * 参数xssEncode
	 * @param request
	 * @return
	 */
	public static int paramXssEncode(HttpServletRequest request) {
		return WebUtils.paramInt(request, AppControllerStatic.APP_API_PARA_XSSENCODE, AppControllerStatic.APP_API_DEF_XSSENCODE);
	}
	
	/**
	 * 参数brandId
	 * @param request
	 * @return
	 */
	public static long paramBrandId(HttpServletRequest request) {
		return WebUtils.paramInt(request, AppControllerStatic.APP_API_PARA_BRANDID, 0);
	}
	
	/**
	 * 参数seriesId
	 * @param request
	 * @return
	 */
	public static long paramSeriesId(HttpServletRequest request) {
		return WebUtils.paramInt(request, AppControllerStatic.APP_API_PARA_SERIESID, 0);
	}
	
	/**
	 * 参数dynaInfoId
	 * @param request
	 * @return
	 */
	public static long paramDynaInfoId(HttpServletRequest request) {
		return WebUtils.paramLong(request, AppControllerStatic.APP_API_PARA_DYNAINFOID, 0);
	}
	
	/**
	 * 参数clubUrl
	 * @param request
	 * @return
	 */
	public static String paramClubUrl(HttpServletRequest request) {
		return WebUtils.param(request, AppControllerStatic.APP_API_PARA_CLUBURL, "");
	}
	
	/**
	 * 参数activityId
	 * @param request
	 * @return
	 */
	public static long paramActivityId(HttpServletRequest request) {
		return WebUtils.paramLong(request, AppControllerStatic.APP_API_PARA_ACTIVITYID, 0);
	}
	
	/**
	 * 参数albumId
	 * @param request
	 * @return
	 */
	public static long paramAlbumId(HttpServletRequest request) {
		return WebUtils.paramLong(request, AppControllerStatic.APP_API_PARA_ALBUMID, 0);
	}
	
	/**
	 * 参数photoId
	 * @param request
	 * @return
	 */
	public static long paramPhotoId(HttpServletRequest request) {
		return WebUtils.paramLong(request, AppControllerStatic.APP_API_PARA_PHOTOID, 0);
	}
	
	/**
	 * 参数content
	 * @param request
	 * @return
	 */
	public static String paramContent(HttpServletRequest request) {
		return WebUtils.param(request, AppControllerStatic.APP_API_PARA_CONTENT, "");
	}
	
	/**
	 * 参数windowname
	 * @param request
	 * @return
	 */
	public static int paramWindowName(HttpServletRequest request){
		return WebUtils.paramInt( request, AppControllerStatic.WINDOW_NAME, 0 );
	}
}
